package com.yh.base.util;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;


/**
 * 
 * 字符串操作通用类
 * 
 * @author  严华
 * @version  [版本号, 2012-11-4]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class StringUtil extends StringUtils {

	@SuppressWarnings("rawtypes")
    public static String replaceParam(String str, Map map, String filterStart, String filterEnd,boolean useSessionFlag) {
		List<String> list = getParamList(str, filterStart, filterEnd);
		for (String item : list) {
			String param = item.substring(filterStart.length(), item.length() - filterEnd.length());
			Object value = map.get(param);
			if (value == null && useSessionFlag) {
				value = map.get(getParam(param));
			}
			if (StringUtil.isValid(value)) {
				str = str.replace(item, value.toString());
			}
		}

		return str;
	}

	public static String getParam(String param) {
		if (param.indexOf("_") != -1) {
			String tmpparam = param.toLowerCase();
			String newParam = "";
			String[] array = tmpparam.split("_");
			for (int i = 0; i < array.length; i++) {
				if (i == 0) {
					newParam = array[i];
				} else {
					newParam += array[i].substring(0, 1).toUpperCase() + array[i].substring(1, array[i].length());
				}
			}
			return newParam;
		} else {
			return param.toLowerCase();
		}
	}

	public static List<String> getParamList(String str, String filterStart, String filterEnd) {
		List<String> resultList = new ArrayList<String>();
		int indexStart = str.indexOf(filterStart);
		int indexEnd = str.indexOf(filterEnd);

		if (indexStart != -1 && indexEnd != -1) {
			String param = str.substring(indexStart, indexEnd + filterEnd.length());
			resultList.add(param);
			String end = str.substring(indexEnd + filterEnd.length(), str.length());
			
			List<String> list = getParamList(end, filterStart, filterEnd);
			if (list.size() > 0) {
				resultList.addAll(list);
			}
		}

		return resultList;
	}

	/**
	 * 
	 * 获取字符串参数中{}参数信息
	 *
	 * @author  严华
	 * @param str
	 * @return [参数说明]
	 * 
	 * @return List<String> [返回类型说明]
	 * @exception throws [异常类型] [异常说明]
	 */
	public static List<String> getParamList(String str) {
		List<String> list = new ArrayList<String>();
		if (!StringUtil.isValid(str) || str.indexOf("{") == -1 || str.indexOf("}") == -1) {
		} else {
			String right = str.substring(str.indexOf("{") + 1, str.length());
			String code = right.substring(0, right.indexOf("}"));
			list.add(code);
			List<String> list2 = getParamList(right.substring(right.indexOf("}") + 1, right.length()));
			for (String item : list2) {
				list.add(item);
			}
		}

		return list;
	}

	/**
	 * 
	 * 返回是否为空,如果是字符串判定，使用isEmpty和isBlank
	 *
	 * @author  严华
	 * @param str
	 * @return [参数说明]
	 * 
	 * @return boolean [返回类型说明]
	 * @exception throws [异常类型] [异常说明]
	 */
	public static boolean isValid(Object str) {
		return (str != null && str.toString().length() > 0);
	}

	/**
	 * 
	 * 将目标对象toString
	 *
	 * @author  严华
	 * @param obj
	 * @return [参数说明]
	 * 
	 * @return String [返回类型说明]
	 * @exception throws [异常类型] [异常说明]
	 */
	public static String asString(Object obj) {
		if (obj == null) {
			return "";
		} else {
			return obj.toString().trim();
		}
	}

	/**
	 * 
	 * 将目标对象toString
	 *
	 * @author  严华
	 * @param obj
	 * @param defaultValue
	 * @return [参数说明]
	 * 
	 * @return String [返回类型说明]
	 * @exception throws [异常类型] [异常说明]
	 */
	public static String asString(Object obj, String defaultValue) {
		if (obj == null) {
			return defaultValue;
		} else {
			return obj.toString().trim();
		}
	}

	/**
	 * 
	 * 获取正则表达式中指定分组的匹配结果<br/>
     * 例如：StringUtil.matchs(qrySql, "\\{(\\w+)\\}", 1)<br/>
     * 返回结果 {\\w+} 匹配后的 \\w+ 内容，因为 \\w+ 出现在正则中第1对()内，为第一组
	 *
	 * @author  严华
	 * @param input
	 * @param regex
	 * @param group
	 * @return [参数说明]
	 * 
	 * @return List<String> [返回类型说明]
	 * @exception throws [异常类型] [异常说明]
	 */
	public static List<String> matchs(String input, String regex, int group) {
		Pattern pattern = Pattern.compile(regex);
		Matcher match = pattern.matcher(input);
		List<String> matches = new ArrayList<String>();
		while (match.find()) {
			matches.add(match.group(group));
		}
		return matches;
	}
}
